/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package service.reportCenter.fgiInventoryReport;

import com.openbrew.openbrewrf.Firkins;
import com.openbrew.openbrewrf.PackageType;
import com.openbrew.openbrewrf.views.FGIView;
import com.openbrew.openbrewrf.views.POSBeerView;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.fgiinventoryreport")
public class FGIInventoryReportREST {

    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public FGIInventoryReportREST() {
    }
    
    @GET
    @Path("getFgiInventoryReport")
    @Produces(MediaType.APPLICATION_XML)
    public FGIInventory runFullInventoryReport() {
        List<InvRow> inProTableArray = new ArrayList<InvRow>();
        List<InvRow> nonProTableArray = new ArrayList<InvRow>();
        List<POSBeerView> posBeerViews = getBeerViews(em);
        List<PackageType> packageTypes = getPackageTypes(em);
        List<Firkins> firkins = getUnsoldFirkins(em);
        List<FGIView> fgis = getFGIs(em);
        //Set up columns for and insert them into each array space.
        for (final POSBeerView beer : posBeerViews) {
            // if beer is in production add it to inPro, else nonPro
            if (beer.getInProduction()) {
                List<InvColumn> columns = new ArrayList<InvColumn>();
                for (final PackageType pkgType : packageTypes) {
                    boolean made = false;
                    for (final FGIView fgi : fgis) {
                        if ((fgi.getFGIBeerID().getbId() == beer.getbId()) && (fgi.getFGIPackageID().getPackageTypeID() == pkgType.getPackageTypeID())) {
                            columns.add(new InvColumn(pkgType, fgi));
                            made = true;
                        }
                    }
                    if (made == false) {
                        FGIView spacerFGI = new FGIView();
                        spacerFGI.setFGIQtyOnHand(0);
                        columns.add(new InvColumn(pkgType, spacerFGI));
                    }
                }
                inProTableArray.add(new InvRow(beer, columns));
            } else {
                List<InvColumn> columns = new ArrayList<InvColumn>();
                for (final PackageType pkgType : packageTypes) {
                    boolean made = false;
                    for (final FGIView fgi : fgis) {
                        if ((fgi.getFGIBeerID().getbId() == beer.getbId()) && (fgi.getFGIPackageID().getPackageTypeID() == pkgType.getPackageTypeID())) {
                            columns.add(new InvColumn(pkgType, fgi));
                            made = true;
                        }
                    }
                    if (made == false) {
                        FGIView spacerFGI = new FGIView();
                        spacerFGI.setFGIQtyOnHand(0);
                        columns.add(new InvColumn(pkgType, spacerFGI));
                    }
                }
                nonProTableArray.add(new InvRow(beer, columns));
            }
        }
        FGIInventory report = new FGIInventory();
        report.setInProductionBeers(inProTableArray);
        report.setNonProductionBeers(nonProTableArray);
        report.setFirkinInventory(firkins);
        return report;
    }

    @GET
    @Path("getInProduction")
    @Produces(MediaType.APPLICATION_XML)
    public List<InvRow> findInProduction() {
        List<InvRow> inProTableArray = new ArrayList<InvRow>();
        List<POSBeerView> posBeerViews = getBeerViews(em);
        List<PackageType> packageTypes = getPackageTypes(em);
        List<FGIView> fgis = getFGIs(em);
        //Set up columns for and insert them into each array space.
        for (final POSBeerView beer : posBeerViews) {
            // if beer is in production add it to inPro, else nonPro
            if (beer.getInProduction()) {
                List<InvColumn> columns = new ArrayList<InvColumn>();
                for (final PackageType pkgType : packageTypes) {
                    boolean made = false;
                    for (final FGIView fgi : fgis) {
                        if ((fgi.getFGIBeerID().getbId() == beer.getbId()) && (fgi.getFGIPackageID().getPackageTypeID() == pkgType.getPackageTypeID())) {
                            columns.add(new InvColumn(pkgType, fgi));
                            made = true;
                        }
                    }
                    if (made == false) {
                        FGIView spacerFGI = new FGIView();
                        spacerFGI.setFGIQtyOnHand(0);
                        columns.add(new InvColumn(pkgType, spacerFGI));
                    }
                }
                inProTableArray.add(new InvRow(beer, columns));
            } 
        }
        return inProTableArray;
    }

    @GET
    @Path("getNonProduction")
    @Produces(MediaType.APPLICATION_XML)
    public List<InvRow> findNonProduction() {
        List<InvRow> nonProTableArray = new ArrayList<InvRow>();
        List<POSBeerView> posBeerViews = getBeerViews(em);
        List<PackageType> packageTypes = getPackageTypes(em);
        List<FGIView> fgis = getFGIs(em);
        //Set up columns for and insert them into each array space.
        for (final POSBeerView beer : posBeerViews) {
            // if beer is in production add it to inPro, else nonPro
            if (!beer.getInProduction()) {
                List<InvColumn> columns = new ArrayList<InvColumn>();
                for (final PackageType pkgType : packageTypes) {
                    boolean made = false;
                    for (final FGIView fgi : fgis) {
                        if ((fgi.getFGIBeerID().getbId() == beer.getbId()) && (fgi.getFGIPackageID().getPackageTypeID() == pkgType.getPackageTypeID())) {
                            columns.add(new InvColumn(pkgType, fgi));
                            made = true;
                        }
                    }
                    if (made == false) {
                        FGIView spacerFGI = new FGIView();
                        spacerFGI.setFGIQtyOnHand(0);
                        columns.add(new InvColumn(pkgType, spacerFGI));
                    }
                }
                nonProTableArray.add(new InvRow(beer, columns));
            } 
        }
        return nonProTableArray;
    }
    
    /***************************************************************************
     * Ref ID List Function.
     **************************************************************************/
    private List<POSBeerView> getBeerViews(final EntityManager en){
        TypedQuery<POSBeerView> query = en.createQuery(
            "SELECT b FROM POSBeerView b", POSBeerView.class);
        return query.getResultList();
    }
    
    private List<PackageType> getPackageTypes(final EntityManager en){
        TypedQuery<PackageType> query = en.createQuery(
            "SELECT p FROM PackageType p", PackageType.class);
        return query.getResultList();
    }
    
    private List<FGIView> getFGIs(final EntityManager en){
        TypedQuery<FGIView> query = en.createQuery(
            "SELECT g FROM FGIView g", FGIView.class);
        return query.getResultList();
    }
    
    private List<Firkins> getUnsoldFirkins(final EntityManager en){
        TypedQuery<Firkins> query = en.createNamedQuery("Firkins.findAllUnsold", Firkins.class);
        return query.getResultList();
    }

    protected EntityManager getEntityManager() {
        return em;
    }
}
